#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    vector<int> findSubstring(string s, vector<string>& words) {
        int left, right;
        int n = words[0].size();
        int cnt = 0;
        vector<int> v;
        unordered_map<string, int> t1;
        unordered_map<string, int> t2;

        for (auto e : words) {
            t1[e]++;
        }
        for (left = 0; left < n; left++) {
            int cur = left;
            t2.clear();
            cnt = 0;
            for (right = cur; right < s.size(); right += n) {
                if (right + n <= s.size()) {
                    string str(s.begin() + right, s.begin() + right + n);
                    t2[str]++;
                    cnt++;
                }

                if (cnt == words.size()) {
                    if (t2 == t1) {
                        if (find(v.begin(), v.end(), cur) == v.end())
                            v.emplace_back(cur);
                    }
                    t2[{s.begin() + cur, s.begin() + cur + n}]--;
                    if (t2[{s.begin() + cur, s.begin() + cur + n}] == 0)
                    {
                        t2.erase({ s.begin() + cur,s.begin() + cur + n });
                    }
                    cnt--;
                    cur += n;
                }
            }
        }
        return v;
    }
};